// xc_drugstore/news/infor.js
// var actbill = require('../js/news.js').news;
// var actbills = require('../js/news.js').eva;
var actbill = require('../model/article_contents')
var evaactbill = require('../model/article_evaluate')
var recactbill = require('../model/article_record')

// var html2wxml = require('../../../html2wxml/html2wxml.js');
var html2wxml = require('../../../html2wxml/html2wxml');

const app = getApp();
Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    allblock: "flex",
    editblock: "none",
    inputfocus: false,
    contents: {},
    requirestatus:"-1",//请求状态-1请求结束 1请求中
  },
  xcloaddata: function (options, config) {
    var that = this;
    that.setData({
      articleid: options.articleid
    })
    
  },
  afterlogin: function (options, config, userinfo) {
    var that=this;
    this.setData({
      member: userinfo
    })
  

    if (userinfo['member_id']!=0) {
 
      that.getmodel(options.articleid)
      that.getpagelist(that, 1)
    }else{
      this.login()
     
    }
  },
  // 登录注册
  login(e) {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  //获取文章详情
  getmodel(id) {
    var that = this;
    actbill.getmodel(id).then(res => {
      if (res["status"] == 1) {
        var obj=res['obj']
        html2wxml.html2wxml('contents', obj.contents, this, 10);
        that.setData({
          article: obj
        })
        if(obj.record.seestatus==-1){
          that.record("see", -1)
        }
      } else {
        wx.showToast({
          icon: "none",
          title: '文章不存在',
        })
      }
    })
  },
  //发送评论
  formSubmit: function (e) {
    console.log(e)
    var that = this;
    var model = {};
    model["message"] = e.detail.value.valuetext;
    model["articleid"] = that.data.articleid;
    // console.log(model)
    // return;
    evaactbill.evaluate(model).then(function (result) {
      if (result['status'] == 1) {
        var obj = result['obj']
        that.setData({
          valuetext: "",
          'article.evanum': obj['evanum'],
        })
        that.getpagelist(that, 1)
        wx.showToast({
          icon: 'none',
          title: "发表成功",
          duration: 2000,
          success: function () {}
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: result['message'],
          duration: 2000
        })
      }

    })
  },
  // 获取信息列表
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }
      strwhere['articleid'] = that.data.articleid;
      strwhere['status'] = 1;
      that.setData({
        modellist: []
      });
    }
    evaactbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
 
    });
  },

  input: function () {
    this.setData({
      allblock: "none",
      editblock: "flex",
      inputfocus: true,
    })
  },
  shuru: function (e) {
    console.log(e);
    this.setData({
      valuetext: e.detail.value,
    })
  },
  blur: function (e) {
    this.setData({
      allblock: "flex",
      editblock: "none",
      inputfocus: false,
    })
  },
  dianzan: function (e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    that.record(dataset.type,dataset.status)
  },
  
  //文章操作
  record(type, status) {
    
    var that=this;
    if(that.data.requirestatus==1){
      return false;
    }else{
      that.setData({
        requirestatus:1
      })
    }
    var model = {};
    model['type'] = type;
    model["articleid"] = that.data.articleid;
    if (status == 1) {
      model["status"] = -1;
    } else {
      model["status"] = 1;
    }
    recactbill.record(model).then(function (result) {
      if (result['status'] == 1) {
        var update = {};
        if (model['type'] == 'good') {
          update["article.record.goodstatus"] = model["status"];
          update["article.good"] = that.data.article.good * 1 + model["status"] ;

        } else if (model['type'] == 'see') {
          update["article.record.seestatus"] = model["status"];
          update["article.see"] = that.data.article.see * 1 + 1 ;

        }
        that.setData(update)
      }
      that.setData({
        requirestatus:-1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    // app.xctool.loadconfig(that).then(function (xrst) {
    //   that.setData({
    //     statusBarHeight: app.globalData.Custom.CustomBar
    //   })
    //   if (xrst["prommember"]) {
    //     xrst["prommember"].then(function () {
    //       that.setData({
    //         member: app.globalData.member,
    //         configdata: app.globalData.configdata.evaluate
    //       })
    //       that.getindex()
    //     })
    //   }
    // })
    // that.getinfo()
  },
  // getindex: function () {
  //   var that = this;

  //   // var id = that.data.newsid;
  //   var model = {};
  //   model["id"] = that.data.newsid;
  //   model["memberid"] = that.data.member.memberinfo.id;

  //   actbill.infor(model).then(function (restmodel) {
  //     if (restmodel["status"] == 1) {
  //       that.setData({
  //         "news": restmodel["obj"].modellist.news,
  //         "count": restmodel["obj"].modellist.count,
  //         "dianzan": restmodel["obj"].modellist.dianzan,
  //         "status": restmodel["obj"].modellist.status,

  //       })
  //     }
  //     console.log("1.newsclassid");
  //     console.log(restmodel);
  //   })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.getpagelist(that, -1)
  },

 
})